import { z } from "zod"

// https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html
export const MessageSchema = {
  ID: "2356550d-e59f-4a7c-9e5b-f55b6d312f05",
  conversationID: "C2C@RBT#006",
  conversationType: "C2C",
  time: 1754536602,
  sequence: 0,
  clientSequence: 0,
  random: 0,
  priority: "Normal",
  nick: "",
  avatar: "avatar",
  isPeerRead: false,
  nameCard: "",
  hasRiskContent: false,
  isPlaceMessage: 0,
  isRevoked: false,
  from: "admin",
  to: "@RBT#006",
  flow: "",
  isSystemMessage: false,
  protocol: "JSON",
  isResend: false,
  isRead: true,
  status: "success",
  atUserList: [],
  cloudCustomData: "",
  isDeleted: false,
  isModified: false,
  clientTime: 1754536602,
  senderTinyID: "",
  needReadReceipt: false,
  version: "0.8.4",
  isBroadcastMessage: false,
  isSupportExtension: false,
  revoker: "",
  revokerInfo: {},
  revokeReason: "",
  payload: {
    text: "",
  },
  type: "TIMTextElem",
  createdAt: 1754536602679,
  updatedAt: 1754536602686,
}

export const MessageStatusSchema = z.enum(["unSend", "fail", "success", "sending", "timeout"])

export const MessageTypeSchema = z.enum([
  "TIMTextElem",
  "TIMRelayElem",
  "TIMImageElem",
  "TIMFileElem",
  "TIMCustomElem",
  "TIMGroupTipElem",
  "TIMGroupSystemNoticeElem",
])

export const DB_MessageSchema = z.object({
  ID: z.string().uuid(),
  conversationID: z.string(),
  conversationType: z.enum(["C2C", "GROUP", "SYSTEM"]),
  time: z.number(),
  sequence: z.number().int().min(0),
  clientSequence: z.number().int().min(0),
  random: z.number().int(),
  priority: z.string(),
  nick: z.string(),
  avatar: z.string(),
  isPeerRead: z.boolean(),
  nameCard: z.string(),
  hasRiskContent: z.boolean(),
  isPlaceMessage: z.number().int(),
  isRevoked: z.boolean(),
  from: z.string(),
  to: z.string(),
  /**
   * @description 消息流向
   * in: 收到
   * out: 发出
   */
  flow: z.enum(["in", "out"]),
  isSystemMessage: z.boolean(),
  protocol: z.enum(["JSON", "XML", "Binary"]),
  isResend: z.boolean(),
  isRead: z.boolean(),
  status: MessageStatusSchema,
  atUserList: z.array(z.string()),
  cloudCustomData: z.string(),
  isDeleted: z.boolean(),
  isModified: z.boolean(),
  clientTime: z.number(),
  senderTinyID: z.string(),
  needReadReceipt: z.boolean(),
  version: z.string(),
  isBroadcastMessage: z.boolean(),
  isSupportExtension: z.boolean(),
  revoker: z.string(),
  revokerInfo: z.record(z.any()).optional(),
  revokeReason: z.string(),
  payload: z.record(z.any()),
  isTimeDivider: z.boolean().optional(),
  type: MessageTypeSchema,
  createdAt: z.number().optional(),
  updatedAt: z.number().optional(),
})

export type DB_Message = z.infer<typeof DB_MessageSchema>

/**
 * @description 消息状态
 * unSend(未发送) fail(发送失败) success(发送成功) sending(发送中) timeout(超时)
 */
export type MessageStatus = z.infer<typeof MessageStatusSchema>

/**
 * @description 消息类型
 * TIMTextElem(文本) TIMRelayElem(转发) TIMImageElem(图片) TIMFileElem(文件) TIMCustomElem(自定义) TIMGroupTipElem(群提示) TIMGroupSystemNoticeElem(群系统提示)
 */
export type MessageType = z.infer<typeof MessageTypeSchema>
